<template>
	<view>
		<view class="detail" v-if="status == 'loading'">
			<u-loadmore :status="status" />
		</view>
		<view class="detail" v-else>
			<view class="detail-title u-margin-bottom-30">
				<text>{{detailInfo.title}}</text>
			</view>
			<view class="detail-header u-margin-bottom-30" style="color: #b5b5b5;">
				<view class="dis-in-block w-50 u-text-left">
					<text>作者：</text><text>{{detailInfo.author}}</text>
				</view>
				<view class="dis-in-block w-50 u-text-right">
					<text>发布于：</text><text>{{detailInfo.date}}</text>
				</view>
			</view>
			<view class="text"> 
				<u-parse :html="detailInfo.demoHtml"></u-parse>
			</view>
		</view>
	</view>
</template>

<script>
	var graceRichText = require("@/comm/richText.js");
	import {
		getAllArticleDetail,
		getAuthor
	} from '@/api/api.js'

	export default {
		onLoad(option) {
			this.getContent(option.id)
		},
		data() {
			return {
				detailInfo:{
					demoHtml: '',
					title: '',
					author:'',
					date:''
				},
				status: 'loading',
			}
		},
		methods: {
			getContent(id) {
				let detailInfo = uni.getStorageSync(`detail-${id}`)
				if(detailInfo == ''){
					getAllArticleDetail(id).then(res => {
						let data = res.data
						this.detailInfo.demoHtml = graceRichText.format(data.content.rendered);
						this.detailInfo.title = data.title.rendered
						this.detailInfo.date = data.date.substring(0,10)
						getAuthor(data.author).then(authorInfo => {
							this.detailInfo.author = authorInfo.data.name
							uni.setStorageSync(`detail-${id}`,this.detailInfo)
							this.status = 'nomore'
						})
					})
				}else{
					this.detailInfo = detailInfo
					this.status = 'nomore'
				}
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	
	.text{
		line-height: 60rpx;
		img{
			text-align: center;
		}
	}
	.detail {
		padding: 20rpx 40rpx;
		background-color: #FFFFFF;

		&-title {
			font-weight: 600;
			font-size: 22px;
		}
	}
</style>
